package com.spring.reactive.controller;

import com.spring.reactive.model.User;
import com.spring.reactive.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RequestMapping("user")
@RestController
public class UserController {


    @Autowired
    private UserRepository userRepository;

    @GetMapping("list")
    public Flux<User> list(){
        return this.userRepository.users();
    }


    @GetMapping("{id}")
    public Mono<User> getUserById(@PathVariable Long id){
        return this.userRepository.user(id);
    }

    @PutMapping
    public Mono<Long> save(User user){
        return this.userRepository.save(user).map(User::getId);
    }

    @PostMapping
    public Mono<User> update(User user){
        return this.userRepository.update(user.getAge(), user.getEmail(), user.getId());
    }


}
